package com.adjaisd.scarecrow.testJava;

import com.adjaisd.scarecrow.dto.DeviceInfoDTO;
import com.adjaisd.scarecrow.entity.Device;
import com.adjaisd.scarecrow.entity.Quota;
import com.adjaisd.scarecrow.mapper.QuotaMapper;
import com.adjaisd.scarecrow.service.DeviceService;
import com.adjaisd.scarecrow.service.DeviceStatusService;
import com.adjaisd.scarecrow.service.QuotaService;
import com.adjaisd.scarecrow.vo.DeviceQuotaVO;
import com.adjaisd.scarecrow.vo.Pager;
import com.adjaisd.scarecrow.vo.QuotaAlarmVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import retrofit2.http.QueryMap;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class testQuota {

    @Autowired
    QuotaService quotaService;

    @Autowired
    DeviceStatusService deviceStatusService;

    @Test
    void testAnalyse() {
        String number = "10002";
        String topic = "device/" + number;
        Map<String, Object> map = new HashMap<>();
        map.put("number", number);
        map.put("temp", "25");
        map.put("hum", "50.2");
        DeviceInfoDTO analysis = quotaService.analysis(topic, map);
        System.out.println(analysis.getDeviceStatusDTO());
        analysis.getQuotaDTOList().forEach(System.out::println);

        deviceStatusService.saveDeviceInfo(analysis.getDeviceStatusDTO());
    }

    @Test
    void selectQuotaAlarmPage() {
        Pager<QuotaAlarmVO> page = quotaService.queryQuotaAlarmPage(1L, 1L, 10L, "", null);
        for (QuotaAlarmVO item : page.getItems()) {
            Quota quota = new Quota();
            BeanUtils.copyProperties(item, quota);
            System.out.println(quota);
            System.out.println(item);
        }
    }

    @Test
    void testQueryDeviceQuotaPage(){
        long userId = 1L;
        long page = 1L;
        long pageSize = 10L;
        String number="";
        String tag = "";
        int state = 1;
        Pager<DeviceQuotaVO> pager = quotaService.queryDeviceQuotaPage(userId, page, pageSize, number, tag, state);
        System.out.println(pager);
    }

    @Autowired
    QuotaMapper quotaMapper;

    @Autowired
    DeviceService deviceService;

    @Test
    void addQuotaData() throws InterruptedException {
        for (int i = 0; i < 50; i++) {
            QueryWrapper<Quota> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(Quota::getNumber, 10000L + i);
            if (quotaMapper.selectList(wrapper).size() == 0) {
                Long number = 10000L + i;
                Long time = new Date().getTime();
                Quota quota1 = new Quota(null, "温度", "℃", "temperature" , "temp", "number", number, "...", "[-5,40]", time, 1L);
                Quota quota2 = new Quota(null, "湿度", "%", "humidity", "hum", "number", number, "...", "[0,100]", time, 1L);
                quotaMapper.insert(quota1);
                quotaMapper.insert(quota2);
                Thread.sleep(20);
            }
        }
    }

}